package asia.lxq.cic.controller;

import asia.lxq.cic.entity.Group;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/group")
@PreAuthorize("hasAnyAuthority('分站管理员','总站管理员')")
public class ManageGroupController {

    @Autowired
    GroupService groupService;

    @GetMapping("/group_list")
    public Result<Object> getGroupList(@RequestParam(required = false,defaultValue = "1") int page,
                                       @RequestParam(required = false,defaultValue = "10") int pageSize){
        return groupService.getGroupList(page, pageSize);
    }

    @PostMapping("/group_add")
    public Result<Object> createGroup(@RequestBody Group group){
        return groupService.createGroup(group);
    }

    @GetMapping("/search_teacher")
    public Result<Object> getTeacherOfCollegeId(String username){
        return groupService.getTeacherOfCollegeId(username);
    }

    @GetMapping("/search_user")
    public Result<Object> getUserOfCollegeId(String username){
        return groupService.getUserOfCollegeId(username);
    }

    @GetMapping("/member_list")
    public Result<Object> getGroupRemember(int groupId){
        return groupService.getGroupMember(groupId);
    }

    @PostMapping("/group_update")
    public Result<Object> groupUpdate(@RequestBody Group group){
        return groupService.groupUpdate(group);
    }

    @PostMapping("/member_add")
    public Result<Object> addRemember(@RequestBody Map<String,String> requestMap){
        String[] userIdsStr = requestMap.get("userIds").split(",");
        int[] userIds=new int[userIdsStr.length];
        int groupId=Integer.parseInt(requestMap.get("groupId"));
        for (int i = 0; i < userIdsStr.length; i++) {
            userIds[i]=Integer.parseInt(userIdsStr[i]);
        }
        return groupService.addMember(userIds,groupId);
    }
    @GetMapping("/member_remove")
    public Result<Object> removeMember(int userId){
        return groupService.removeMember(userId);
    }
}
